package com.csu.mining.activity;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.csu.mining.Fragment.PhotoFragment;
import com.csu.mining.R;
import com.csu.mining.adapters.MiningPicAdapter;

import java.util.ArrayList;
import java.util.List;

public class PictureActivity extends BaseActivity {

    private int order;
    private void initData() {
        Bundle mBundle = getIntent().getExtras();
        if (mBundle != null && mBundle.getSerializable("order") != null) {
            order = (int)mBundle.getSerializable("order");
            Log.e("VideoActivity","the order is "+ order);
        }else{
            Log.e("VideoActivity","the order is none");
            Toast t = Toast.makeText(this,"参数错误，即将返回", Toast.LENGTH_LONG);
            t.show();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            onBackPressed();
        }
    }

    protected void initImage(){
        List<PhotoFragment> mList = new ArrayList<>();
        for(int i = 1;i< 6;i++) {
            PhotoFragment photoFragment = new PhotoFragment();
            int count = i;
            String url = "http://www.forfunfunfun.cn/File/picture/" + order + "/" + count + ".PNG";
            Log.d("PictureActivity", "initImage: "+ url);
            photoFragment.newInstance(url);
            mList.add(photoFragment);
        }
        ViewPager viewPager = fd(R.id.viewpager);
        //MiningPicAdapter miningPicAdapter = new MiningShowPicAdapter(,mList);

        //viewPager.setAdapter(miningPicAdapter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture);
        initData();
        String[] arrWays = {
                this.getString(R.string.way1),this.getString(R.string.way2),
                this.getString(R.string.way3),this.getString(R.string.way4),
                this.getString(R.string.way5),this.getString(R.string.way6),
                this.getString(R.string.way7),this.getString(R.string.way8)
        };
        initNavBar(false,arrWays[order]);
        initImage();
    }

    public static class MiningShowPicAdapter extends FragmentPagerAdapter {

        List<PhotoFragment> mFragmentList;
        public MiningShowPicAdapter(FragmentManager fm, List<PhotoFragment> list) {
            super(fm);
            mFragmentList = list;
        }

        @Override
        public Fragment getItem(int position) {
            if (mFragmentList != null && position < mFragmentList.size()) {
                return mFragmentList.get(position);
            }
            return null;
        }

        @Override
        public int getCount() {
            if (mFragmentList == null) {
                return 0;
            }
            return mFragmentList.size();
        }
    }
}
